package com.ruoyi.yl.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.yl.domain.YlChangePrice;
import com.ruoyi.yl.domain.YlDrug;
import com.ruoyi.yl.domain.dto.DrugDTO;
import com.ruoyi.yl.mapper.DrugMapper;
import com.ruoyi.yl.mapper.YlDrugChangePriceMapper;
import com.ruoyi.yl.service.DrugChangePriceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

/**
 * @Author: linKe
 * @Date: 2022/2/19 9:29
 * @FileName: DrugChangePriceServiceImpl
 * @version: 1.0
 * @Description: 调价管理
 */
@Slf4j
@Service
@Transactional(readOnly = true)
public class DrugChangePriceServiceImpl implements DrugChangePriceService {

    @Resource
    private YlDrugChangePriceMapper drugChangePriceMapper;

    @Resource
    private DrugMapper drugMapper;


    /**
     * 查询所有药品
     * 无条件直接查询
     * @return
     */
    @Override
    public List<YlDrug> getAllDrug() {
        return drugMapper.selectYlDrugList(null);
    }

    /**
     * 根据类型查药品
     * @param id
     * @return
     */
    @Override
    public List<YlDrug> getTypeDrug(Long id) {
        return drugMapper.selectList(new QueryWrapper<YlDrug>().eq("drug_type",id));
    }

    /**
     * 模糊查询
     */
    @Override
    public List<YlDrug> getLikeDrug(YlDrug dsc) {

        return drugMapper.selectYlDrugList(dsc);
    }

    /**
     * 根据药品id查调价详情
     * drugChangePriceMapper.getChangePriceByDrugid(id);
     */
    @Override
    public List<YlChangePrice> getChangePriceByDrugid(Long id) {

        return drugChangePriceMapper.selectList(new QueryWrapper<YlChangePrice>().eq(true,"drug_no",id));

    }

    /**
     * 根据药品id修改零售价
     * 也就是药品编号
     */
    @Override
    @Transactional(isolation = Isolation.REPEATABLE_READ,rollbackFor = Exception.class)
    public void updateDrugSalePrice(Long id, BigDecimal nowPrice) {
        log.info("传入的药品id：{}",id);
        log.info("传入的药品新价格：{}",nowPrice);

        ///BigDecimal oldPrice=drugChangePriceMapper.getOneById(id);
        YlDrug number = drugMapper.selectOne(new QueryWrapper<YlDrug>().eq("number", id));
        // 查询零售价
        BigDecimal oldPrice = number.getSalePrice();
        // 将零售价转成double
        Double dou1=new Double(String.valueOf(nowPrice));
        Double dou2=new Double(String.valueOf(oldPrice));
        // 使用BigDecimal类型进行计算
        BigDecimal differ=new BigDecimal(dou1-dou2);
        log.info("differ：{}",differ);
        Double dou3=new Double(String.valueOf(differ))/dou2;
        log.info("dou3：{}",dou3);

        // 保留两位小数
        DecimalFormat df = new DecimalFormat("0.00");
        log.info("df：{}",df);

        BigDecimal rate=new BigDecimal(df.format(dou3));
        log.info("rate：{}",rate);

        // 动态更新药品价格
        drugChangePriceMapper.updateYlChangePrice(new YlChangePrice().setDrugNo(id).setOldSalePrice(oldPrice).setDiffer(differ).setRate(rate));

        // 根据药品编号更新零售价 调价次数加一
        // 数据插入失败
        drugMapper.updateYlDrugNo(new YlDrug().setNumber(id).setSalePrice(nowPrice).setAdjustCount(number.getAdjustCount()+1));
    }
}